package com.cqut.forum.dao.imp;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.transform.Transformers;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.cqut.forum.dao.BaseDao;

/**
 * base dao的实现类，提供基本方法的实现
 * @author yangxun
 */
@Transactional
@Component
public abstract class BaseDaoImp implements BaseDao{
	
	@Resource
	private HibernateTemplate hibernateTemplate;
	
	public <T> T delete(int id,Class<T> clazz) {
		T t = (T) hibernateTemplate.get(clazz, id);
		hibernateTemplate.delete(t);
		return t;
	}

	public <T> T get(int id,Class<T> clazz) {
		T t = (T) hibernateTemplate.get(clazz, id);
		return t;
	}

	@SuppressWarnings("unchecked")
	public <T> List<T> get(int index, int size,Class<T> clazz) {
		Session session = hibernateTemplate.getSessionFactory().getCurrentSession();
		Query query = session.createQuery("select * from "+getClassName(clazz.getName()));
		query.setFirstResult((index-1)*size);
		query.setMaxResults(size);
		return query.list();
	}

	public <T> T save(T t) {
		hibernateTemplate.save(t);
		return t;
	}
	public <T> T update(T t){
		hibernateTemplate.update(t);
		return t;
	}
	@SuppressWarnings("unchecked")
	public <T> int size(Class<T> clazz) {
		List<Long> list = hibernateTemplate.find("select count(*) from "+getClassName(clazz.getName()));
		return list.get(0).intValue();
	}

	protected Query getQuery(String hql) {
		Session session = hibernateTemplate.getSessionFactory().getCurrentSession();
		Query query = session.createQuery(hql);
		return query;
	}
	protected Query getSqlQuery(String sql) {
		Session session = hibernateTemplate.getSessionFactory().getCurrentSession();
		Query query = session.createSQLQuery(sql).setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
		//Query query = session.createSQLQuery(sql);
		return query;
	}
	
	protected List<?> excuteHql(String hql,List<?> parameters,Map<String, Integer> pages) {
		Query query = setParameters(getQuery(hql),parameters,pages);
		
		return query.list();
	}
	
	protected List<?> excuteSql(String sql,List<?> parameters,Map<String,Integer> pages) {
		Query query = setParameters(getSqlQuery(sql), parameters,pages);
		return query.list();
	}
	
	private Query setParameters(Query query,List<?> parameters,Map<String,Integer> pages){
		if(parameters != null &&parameters.size()>0){
			for(int i=0;i<parameters.size();i++){
				if(parameters.get(i) instanceof Integer){
					query.setInteger(i, (Integer)parameters.get(i));
				}
				else if(parameters.get(i) instanceof String){
					query.setString(i, (String)parameters.get(i));
				}
				else if(parameters.get(i) instanceof Date){
					query.setDate(i, (Date)parameters.get(i));
				}
				else if(parameters.get(i) instanceof Boolean){
					query.setBoolean(i, (Boolean)parameters.get(i));
				}
			}
		}
		if(pages !=null){
			query.setFirstResult(pages.get("index")*pages.get("size"));
			query.setMaxResults(pages.get("size"));
		}
		return query;
	}
	
	/**
	 * 获取类名
	 * @param str 类文件的全路径
	 * @return 返回截取到的类名
	 */
	private static String getClassName(String str){
		//只获取类的名称
		String className = str.substring(str.lastIndexOf('.')+1, str.length());
		return className;
	}
	public HibernateTemplate getHibernateTemplate() {
		return hibernateTemplate;
	}
	public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
		this.hibernateTemplate = hibernateTemplate;
	}
}
